Conditions | 1 |
Paths | 3 |
Total Lines | 153 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /* global API */ |
||
36 | .controller('SetupCtrl', ['$scope', '$timeout', '$location', '$rootScope', 'StepsService', 'notify', function ($scope, $timeout, $location, $rootScope, StepsService, notify) { |
||
37 | $scope.settings = { |
||
38 | nextcloud_host: '', |
||
39 | nextcloud_username: '', |
||
40 | nextcloud_password: '', |
||
41 | ignoreProtocol: true, |
||
42 | ignoreSubdomain: true, |
||
43 | ignorePath: true, |
||
44 | generatedPasswordLength: 12, |
||
45 | remember_password: true, |
||
46 | vault_password: '', |
||
47 | refreshTime: 60, |
||
48 | default_vault: {}, |
||
49 | master_password: '', |
||
50 | disableAutoFill: false, |
||
51 | disablePasswordPicker: false, |
||
52 | disable_browser_autofill: true, |
||
53 | debug: false, |
||
54 | accounts: [] |
||
55 | }; |
||
56 | $scope.vaults = []; |
||
57 | |||
58 | $rootScope.$broadcast('hideHeader'); |
||
59 | $rootScope.setup = true; |
||
60 | $scope.gogo = function (to) { |
||
61 | StepsService.steps().goTo(to); |
||
62 | }; |
||
63 | notify.config({ |
||
64 | 'position': 'left', |
||
65 | 'duration': 2500 |
||
66 | }); |
||
67 | |||
68 | $scope.check = { |
||
69 | server: function (callback) { |
||
70 | if(!$scope.settings.nextcloud_host || !$scope.settings.nextcloud_username || !$scope.settings.nextcloud_password){ |
||
71 | $scope.errors.push(API.i18n.getMessage('invalid_server_settings')); |
||
72 | callback(false); |
||
73 | return; |
||
74 | } |
||
75 | $scope.settings.nextcloud_host = $scope.settings.nextcloud_host.replace(/\/$/, ""); |
||
76 | PAPI.host = $scope.settings.nextcloud_host; |
||
77 | PAPI.username = $scope.settings.nextcloud_username; |
||
78 | PAPI.password = $scope.settings.nextcloud_password; |
||
79 | PAPI.getVaults(function (vaults) { |
||
80 | if (vaults.hasOwnProperty('error')) { |
||
81 | var errors = API.i18n.getMessage('invalid_response_from_server', [vaults.result.status, vaults.result.statusText]); |
||
82 | $scope.errors.push(errors); |
||
83 | notify(errors); |
||
84 | callback(false); |
||
85 | } |
||
86 | else { |
||
87 | $scope.vaults = vaults; |
||
88 | callback(true); |
||
89 | } |
||
90 | $scope.$apply(); |
||
91 | }); |
||
92 | }, |
||
93 | vault: function (callback) { |
||
94 | try { |
||
95 | PAPI.decryptString($scope.settings.default_vault.challenge_password, $scope.settings.vault_password); |
||
96 | callback(true); |
||
97 | } |
||
98 | catch (e) { |
||
99 | $scope.errors.push(); |
||
100 | notify(API.i18n.getMessage('invalid_vault_password')); |
||
101 | callback(false); |
||
102 | } |
||
103 | }, |
||
104 | master: function (callback) { |
||
105 | if ($scope.settings.master_password.trim() !== '') { |
||
106 | callback(true); |
||
107 | } else { |
||
108 | notify(API.i18n.getMessage('empty_master_key')); |
||
109 | callback(false); |
||
110 | } |
||
111 | } |
||
112 | }; |
||
113 | $scope.saving = false; |
||
114 | $scope.next = function () { |
||
115 | $scope.saving = true; |
||
116 | $scope.errors = []; |
||
117 | $timeout(function () { |
||
118 | var step = StepsService.getCurrent().name; |
||
119 | var check = $scope.check[step]; |
||
120 | if (typeof check === "function") { |
||
121 | check(function (result) { |
||
122 | $scope.saving = false; |
||
123 | if (result) { |
||
124 | $scope.errors = []; |
||
125 | $scope.$apply(); |
||
126 | StepsService.steps().next(); |
||
127 | } |
||
128 | $timeout(function () { |
||
129 | $scope.errors = []; |
||
130 | $scope.$apply(); |
||
131 | }, 5000); |
||
132 | }); |
||
133 | } |
||
134 | else { |
||
135 | $scope.saving = false; |
||
136 | StepsService.steps().next(); |
||
137 | } |
||
138 | }, 10); |
||
139 | }; |
||
140 | |||
141 | $scope.finished = function () { |
||
142 | var settings = angular.copy($scope.settings); |
||
143 | var master_password = settings.master_password; |
||
144 | var master_password_remember = settings.master_password_remember; |
||
145 | var account = { |
||
146 | nextcloud_host: settings.nextcloud_host, |
||
147 | nextcloud_username: settings.nextcloud_username, |
||
148 | nextcloud_password: settings.nextcloud_password, |
||
149 | vault: settings.default_vault, |
||
150 | vault_password: settings.vault_password, |
||
151 | }; |
||
152 | settings.accounts.push(account); |
||
153 | delete settings.master_password; |
||
154 | delete settings.master_password_remember; |
||
155 | delete settings.nextcloud_host; |
||
156 | delete settings.nextcloud_username; |
||
157 | delete settings.nextcloud_password; |
||
158 | delete settings.vault_password; |
||
159 | delete settings.default_vault; |
||
160 | |||
161 | $scope.saving = true; |
||
162 | console.log(settings); |
||
|
|||
163 | |||
164 | |||
165 | API.runtime.sendMessage(API.runtime.id, { |
||
166 | method: "setMasterPassword", |
||
167 | args: {password: master_password, savePassword: master_password_remember} |
||
168 | }) |
||
169 | .then(function () { |
||
170 | API.runtime.sendMessage(API.runtime.id, { |
||
171 | method: "saveSettings", |
||
172 | args: settings |
||
173 | }).then(function () { |
||
174 | setTimeout(function () { |
||
175 | $rootScope.setup = false; |
||
176 | $rootScope.$broadcast('showHeader'); |
||
177 | window.location = '#!/'; |
||
178 | API.runtime.sendMessage(API.runtime.id, { |
||
179 | method: "closeSetupTab" |
||
180 | }); |
||
181 | $scope.saving = false; |
||
182 | }, 750); |
||
183 | }); |
||
184 | }); |
||
185 | |||
186 | |||
187 | }; |
||
188 | }]); |
||
189 | }()); |
||
191 |